/**
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 * <p>
 * CjCompareJobResultService 文件对比结果
 * <p>
 * 修改纪录 2023-02-21 版本：1.0 zhanglongfei 创建。
 *
 * @version 版本：1.0
 * @author 作者：zhanglongfei 创建日期：2023-02-21
 */
package com.topscomm.mis.cbb.file.compare.cj.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.Config;
import com.topscomm.cbo.service.ICboAttachmentService;
import com.topscomm.mis.cbb.file.compare.cj.CjSystemConst;
import com.topscomm.mis.cbb.file.compare.cj.pojo.CjCompareJobResultEntity;
import com.topscomm.mis.cbb.file.compare.cj.pojo.dto.CompareItemsDto;
import com.topscomm.mis.cbb.file.compare.word.handler.BasicCompareHandler;
import com.topscomm.mis.cbb.file.compare.word.handler.IDocumentCompareHandler;
import com.topscomm.mis.cbb.file.compare.word.service.PoiCommentService;
import com.topscomm.mis.cbb.file.compare.word.util.CbbFileUtil;
import com.topscomm.mis.cbb.file.compare.word.util.ImageMergeBean;
import com.topscomm.mis.cbb.file.compare.word.util.ImageUtil;
import com.topscomm.mis.cbb.file.compare.word.util.PoiWordUtil;
import com.topscomm.mis.cbb.file.compare.word.util.aspose.AsposeFileConvertToImg;
import com.topscomm.pub.util.ConvertUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mis.cbb.file.compare.cj.service.ICjCompareJobResultService;
import com.topscomm.mis.cbb.file.compare.cj.service.impl.auto.CjCompareJobResultServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@Service(value = "cjCompareJobResultService")
@Scope(value = "singleton")
@Slf4j
public class CjCompareJobResultService extends CjCompareJobResultServiceAuto implements
    ICjCompareJobResultService {

    @Autowired
    private List<IDocumentCompareHandler> documentCompareHandlerList;
    @Autowired
    private ICboAttachmentService cboAttachmentService;

    /**
     * @param file1:
     * @param file2:
     * @param paraMap:
     * @description: 比较word文档内容差异
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 14:05
     * @modify:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> compareWords(File file1, File file2, Map<String, Object> paraMap,
        CompareItemsDto compareItemsDto) {

        System.out.println("====文档对比开始：[" + System.currentTimeMillis() + "]====");

        XWPFDocument document1 = null, document2 = null;
        FileInputStream fileInputStream1 = null, fileInputStream2 = null;
        File fileOut1 = null, fileOut2 = null;
        try {
            fileInputStream1 = new FileInputStream(file1);
            fileInputStream2 = new FileInputStream(file2);
            document1 = new XWPFDocument(fileInputStream1);
            document2 = new XWPFDocument(fileInputStream2);

            String sourceId = ConvertUtil.convertToString(paraMap.get("sourceId"));
            String firstImageTitle = ConvertUtil.convertToString(paraMap.get("firstImageTitle"));
            String secondImageTitle = ConvertUtil.convertToString(paraMap.get("secondImageTitle"));
            String firstResultSourceType = ConvertUtil
                .convertToString(paraMap.get("firstResultSourceType"));
            String secondResultSourceType = ConvertUtil
                .convertToString(paraMap.get("secondResultSourceType"));
            String imageResultSourceType = ConvertUtil
                .convertToString(paraMap.get("imageResultSourceType"));
            String zipResultSourceType = ConvertUtil
                .convertToString(paraMap.get("zipResultSourceType"));

            System.out.println("====handler开始：[" + System.currentTimeMillis() + "]====");
            List<CjCompareJobResultEntity> resultList = new ArrayList<>();
            for (IDocumentCompareHandler handler : documentCompareHandlerList) {
                resultList.addAll(handler.doCompare(document1, document2, compareItemsDto));
            }
            System.out.println("====handler结束：[" + System.currentTimeMillis() + "]====");
            // 先保存文件，便于拷贝的其他表格/段落对象，可以深拷贝
            String path = Config.getInstance().getValue("file.temp.path", "/temp/");
            path += sourceId + "/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdir();
            }
            int diffCount = resultList.size();
            List<String> pageList = new ArrayList<>();
            for(CjCompareJobResultEntity compareJobResultEntity : resultList) {
                String pageNo = compareJobResultEntity.getPageno()+"";
                if(!pageList.contains(pageNo)) {
                    pageList.add(pageNo);
                }
            }
            String diffPage = pageList.stream().collect(Collectors.joining(","));
            String time = DateUtil.format(new Date(), "yyyyMMddHHmm");
            String subFix = "_" + time + "_" + diffCount + "处差异" + "_批注版.docx";
            String outFile1 =
                path + file1.getName().toLowerCase(Locale.ROOT).replace(".docx", subFix),
                outFile2 = path + file2.getName().toLowerCase(Locale.ROOT).replace(".docx", subFix);
            saveFile(document1, outFile1);
            saveFile(document2, outFile2);
            closeStream(document1);
            closeStream(document2);
            closeStream(fileInputStream1);
            closeStream(fileInputStream2);

            fileInputStream1 = new FileInputStream(outFile1);
            document1 = new XWPFDocument(fileInputStream1);
            fileInputStream2 = new FileInputStream(outFile2);
            document2 = new XWPFDocument(fileInputStream2);
            PoiCommentService commentService1 = new PoiCommentService(document1);
            PoiCommentService commentService2 = new PoiCommentService(document2);
            createComment(document1, document2, commentService1, commentService2, resultList,
                firstImageTitle, secondImageTitle);
            System.out.println("====添加批注结束：[" + System.currentTimeMillis() + "]====");
            saveFile(document1, outFile1);
            saveFile(document2, outFile2);
            System.out.println("====上传批注附件开始：[" + System.currentTimeMillis() + "]====");
            saveDb(resultList, paraMap);

            fileOut1 = new File(outFile1);
            fileOut2 = new File(outFile2);
            Map<String, Object> map = new HashMap<>(2);
            map.put("sourceId", paraMap.get("sourceId"));
            map.put("sourceType", firstResultSourceType);
            cboAttachmentService.executeUpload(map, fileOut1);
            map.put("sourceType", secondResultSourceType);
            cboAttachmentService.executeUpload(map, fileOut2);
            System.out.println("====上传批注附件结束：[" + System.currentTimeMillis() + "]====");
            if (paraMap.containsKey("needCheck") && "1"
                .equals(ConvertUtil.convertToString(paraMap.get("needCheck")))) {
                int pageNo = Math.max(document1.getProperties().getExtendedProperties().getPages(),
                    document2.getProperties().getExtendedProperties().getPages());
                generateCheckList(pageNo, paraMap);
            }
            System.out.println("====生成图片，压缩并上传附件开始：[" + System.currentTimeMillis() + "]====");
            // 比对结果生成压缩包,并保存至附件
            generateZipResult(outFile2, outFile1, sourceId, firstImageTitle, secondImageTitle,
                imageResultSourceType, zipResultSourceType);
            System.out.println("====生成图片，压缩并上传附件结束：[" + System.currentTimeMillis() + "]====");
            System.out.println("====文档对比结束：[" + System.currentTimeMillis() + "]====");
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("diffCount",diffCount);
            resultMap.put("diffPage",diffPage);
            return resultMap;
        } catch (Exception ex) {
            throw new ServiceException(ex);
        } finally {
            closeStream(document1);
            closeStream(document2);
            closeStream(fileInputStream1);
            closeStream(fileInputStream2);
            FileUtil.del(fileOut1.getParent());
        }
    }

    /**
     * @param paraMap:
     * @param checkList:
     * @description: 保存CheckList
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/27 20:06
     * @modify:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCheckList(Map<String, Object> paraMap, List<Map<String, Object>> checkList) {
        String sourceApp = ConvertUtil.convertToString(paraMap.get("sourceApp"));
        String sourceType = ConvertUtil.convertToString(paraMap.get("sourceType"));
        String sourceId = ConvertUtil.convertToString(paraMap.get("sourceId"));
        StringBuilder searchSql = new StringBuilder();
        searchSql.append(" sourceapp = '" + sourceApp + "' ");
        searchSql.append(" and sourcetype = '" + sourceType + "' ");
        searchSql.append(" and sourceid = '" + sourceId + "' ");
        searchSql.append(
            " and difftype = '" + CjSystemConst.CjCompareJobResultDiffType.CheckList + "' ");
        List<CjCompareJobResultEntity> oldList = queryByWhere(searchSql.toString());
        List<BasicEntity> updateList = new ArrayList<>();
        for (CjCompareJobResultEntity result : oldList) {
            Optional<Map<String, Object>> transMap = checkList.stream()
                .filter(a -> result.getId() == ConvertUtil.convertToLong(a.get("id"))).findFirst();
            if (!transMap.isPresent()) {
                throw new ServiceException("第" + result.getPageno() + "页没有勾选结果");
            }
            result.setCheckstate(ConvertUtil.convertToString(transMap.get().get("checkstate")));
            updateList.add(result);
        }
        updateBatch(updateList);
    }

    /**
     * description 生成最终的结果压缩包
     *
     * @param secondFileName   文件2名称
     * @param firstFileName    文件1名称
     * @param sourceId         来源id
     * @param firstImageTitle  图片1标题
     * @param secondImageTitle 图片2标题
     * @param imageResultSourceType  图片结果存储类型
     * @param zipResultSourceType 压缩包结构存储类型
     * @return void
     * @author wxm
     * @date 2023-06-16 10:11:09
     * @modify
     */
    private void generateZipResult(String secondFileName, String firstFileName, String sourceId,
        String firstImageTitle, String secondImageTitle, String imageResultSourceType,
        String zipResultSourceType) {
        System.out.println("====生成图片开始：[" + System.currentTimeMillis() + "]====");
        // 将结果文档转换成图片
        String secondFileImage = AsposeFileConvertToImg.convertOneImg(secondFileName);
        String firstFileImage = AsposeFileConvertToImg.convertOneImg(firstFileName);
        // 图片合并

        List<ImageMergeBean> imageMergeListSecondFile = new ArrayList<>();
        ImageMergeBean mergeBeanSecondFile = new ImageMergeBean();
        mergeBeanSecondFile.setPath(secondFileImage);
        mergeBeanSecondFile.setTitle(secondImageTitle);
        imageMergeListSecondFile.add(mergeBeanSecondFile);

        List<ImageMergeBean> imageMergeListFirstFile = new ArrayList<>();
        ImageMergeBean mergeBeanFirstFile = new ImageMergeBean();
        mergeBeanFirstFile.setPath(firstFileImage);
        mergeBeanFirstFile.setTitle(firstImageTitle);
        imageMergeListFirstFile.add(mergeBeanFirstFile);
        System.out.println("====生成图片结束：[" + System.currentTimeMillis() + "]====");
        System.out.println("====上传图片开始：[" + System.currentTimeMillis() + "]====");
        // 获取当前文件的路径
        File file = new File(secondFileName);
        System.out.println("====合并图片开始：[" + System.currentTimeMillis() + "]====");
        String resultJpgPath = ImageUtil
            .mergeMultiColunm(file.getParent() + "/", imageMergeListFirstFile,
                imageMergeListSecondFile);
        // 合并后的图片上传至附件
        System.out.println("====合并图片结束：[" + System.currentTimeMillis() + "]====");
        Map<String, Object> map = new HashMap<>(2);
        map.put("sourceId", sourceId);
        map.put("sourceType", imageResultSourceType);
        cboAttachmentService.executeUpload(map, new File(resultJpgPath));
        System.out.println("====上传图片结束：[" + System.currentTimeMillis() + "]====");
        // 删除转换出的两张单独的图片
        deleteFile(new File(secondFileImage));
        deleteFile(new File(firstFileImage));
        // 压缩文件
        File zipFile = ZipUtil.zip(new File(file.getParent()));
        // 文件下载到本地进行调试
        System.out.println("====下载压缩文件开始：[" + System.currentTimeMillis() + "]====");
        CbbFileUtil.downloadFile(zipFile);
        System.out.println("====下载压缩文件结束：[" + System.currentTimeMillis() + "]====");
        // 压缩包上传至附件
        System.out.println("====上传压缩包开始：[" + System.currentTimeMillis() + "]====");
        map.put("sourceType", zipResultSourceType);
        cboAttachmentService.executeUpload(map, zipFile);
        // 删除压缩包
        deleteFile(zipFile);
        System.out.println("====上传压缩包结束：[" + System.currentTimeMillis() + "]====");
    }





    /**
     * @param resultList:
     * @param paraMap:
     * @description: 保存数据到数据库
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 14:09
     * @modify:
     */
    private void saveDb(List<CjCompareJobResultEntity> resultList, Map<String, Object> paraMap) {
        String sourceApp = ConvertUtil.convertToString(paraMap.get("sourceApp"));
        String sourceType = ConvertUtil.convertToString(paraMap.get("sourceType"));
        String sourceId = ConvertUtil.convertToString(paraMap.get("sourceId"));
        List<BasicEntity> list = resultList.stream().map(item -> {
            item.setCheckstate("0");
            item.setSourceapp(sourceApp);
            item.setSourceid(sourceId);
            item.setSourcetype(sourceType);
            return (BasicEntity) item;
        }).collect(Collectors.toList());
        insertBatch(list);
    }

    /**
     * @param pageNo:
     * @param paraMap:
     * @description: 生成checkList
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/27 13:51
     * @modify:
     */
    private void generateCheckList(int pageNo, Map<String, Object> paraMap) {
        List<BasicEntity> list = new ArrayList<>();
        String sourceApp = ConvertUtil.convertToString(paraMap.get("sourceApp"));
        String sourceType = ConvertUtil.convertToString(paraMap.get("sourceType"));
        String sourceId = ConvertUtil.convertToString(paraMap.get("sourceId"));
        BasicCompareHandler handler = new BasicCompareHandler();
        for (int i = 1; i <= pageNo; i++) {
            CjCompareJobResultEntity item = handler.createNewResultEntity();
            item.setPageno(i);
            item.setCheckstate("0");
            item.setSourceapp(sourceApp);
            item.setSourceid(sourceId);
            item.setSourcetype(sourceType);
            item.setDifftype(CjSystemConst.CjCompareJobResultDiffType.CheckList);
            item.setDescription("文档第" + i + "页内容");
            list.add(item);
        }
        insertBatch(list);
    }

    /**
     * @param document1:
     * @param document2:
     * @param commentService1:
     * @param commentService2:
     * @param resultList:
     * @description: 根据异常添加批注
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:57
     * @modify:
     */
    private void createComment(XWPFDocument document1, XWPFDocument document2,
        PoiCommentService commentService1, PoiCommentService commentService2,
        List<CjCompareJobResultEntity> resultList, String firstImageTitle,
        String secondImageTitle) {
        for (CjCompareJobResultEntity re : resultList) {
            re.setCheckstate("0");

            if (re.getTableno() >= 0) {
                XWPFTable table1 = document1.getTableArray(re.getTableno());
                XWPFTable table2 = document2.getTableArray(re.getTableno());
                if (re.getRowno() >= 0) {
                    XWPFTableRow row1 = table1.getRow(re.getRowno());
                    XWPFTableRow row2 = table2.getRow(re.getRowno());
                    if (re.getColumnno() >= 0) {
                        XWPFTableCell cell1 = row1.getCell(re.getColumnno());
                        XWPFTableCell cell2 = row2.getCell(re.getColumnno());
                        if (re.getParagraphno() >= 0) {
                            addParagraphRunComment(commentService1, commentService2, re,
                                cell1.getParagraphArray(re.getParagraphno()),
                                cell2.getParagraphArray(re.getParagraphno()), firstImageTitle,
                                secondImageTitle);
                        } else {
                            addCellComment(cell1, cell2, re, commentService1, commentService2,
                                firstImageTitle,
                                secondImageTitle);
                        }
                    } else {
                        addRowComment(row1, row2, re, commentService1, commentService2,
                            firstImageTitle,
                            secondImageTitle);
                    }
                } else {
                    addTableComment(table1, table2, re, commentService1, commentService2,
                        firstImageTitle,
                        secondImageTitle);
                }
            } else if (re.getParagraphno() >= 0) {
                addParagraphRunComment(commentService1, commentService2, re,
                    document1.getParagraphArray(re.getParagraphno()),
                    document2.getParagraphArray(re.getParagraphno()), firstImageTitle,
                    secondImageTitle);
            }
        }
    }

    /**
     * description 段落和run样式批注
     *
     * @param commentService1
     * @param commentService2
     * @param re
     * @param paragraphArray
     * @param paragraphArray2
     * @param firstImageTitle
     * @param secondImageTitle
     * @return void
     * @author wxm
     * @date 2023-06-16 10:44:55
     * @modify
     */
    private void addParagraphRunComment(PoiCommentService commentService1,
        PoiCommentService commentService2, CjCompareJobResultEntity re,
        XWPFParagraph paragraphArray, XWPFParagraph paragraphArray2, String firstImageTitle,
        String secondImageTitle) {
        if (re.getRunno() >= 0) {
            System.out.println(paragraphArray2.getRuns().size());
            if(paragraphArray.getRuns().size()==0 || paragraphArray2.getRuns().size()==0){
                return;
            }
            XWPFRun run1 = paragraphArray.getRuns().get(re.getRunno());
            XWPFRun run2 = paragraphArray2.getRuns().get(re.getRunno());
            addRunComment(run1, run2, re, commentService1, commentService2, firstImageTitle,
                secondImageTitle);
        } else {
            addParagraphComment(paragraphArray, paragraphArray2, re, commentService1,
                commentService2, firstImageTitle, secondImageTitle);
        }
    }

    /**
     * @param run1:
     * @param run2:
     * @param re:
     * @param firstImageTitle:
     * @param secondImageTitle:
     * @description: 给Run添加批注
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:56
     * @modify:
     */
    private void addRunComment(XWPFRun run1, XWPFRun run2, CjCompareJobResultEntity re,
        PoiCommentService commentService1, PoiCommentService commentService2,
        String firstImageTitle,
        String secondImageTitle) {
        commentService1.insertCommentToRun(run1, secondImageTitle + re.getDoccontent2());
        commentService2.insertCommentToRun(run2, firstImageTitle + re.getDoccontent1());
        PoiWordUtil.assignColor(run1, CjSystemConst.CjCompareJobResultColor.Diff);
        PoiWordUtil.assignColor(run2, CjSystemConst.CjCompareJobResultColor.Diff);
    }

    /**
     * @param paragraph1:
     * @param paragraph2:
     * @param firstImageTitle:
     * @param secondImageTitle:
     * @description: 给段落添加批注
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:56
     * @modify:
     */
    private void addParagraphComment(XWPFParagraph paragraph1, XWPFParagraph paragraph2,
        CjCompareJobResultEntity re, PoiCommentService commentService1,
        PoiCommentService commentService2, String firstImageTitle,
        String secondImageTitle) {
        commentService1
            .insertCommentToParagraph(paragraph1, secondImageTitle + re.getDoccontent2());
        commentService2.insertCommentToParagraph(paragraph2, firstImageTitle + re.getDoccontent1());
        if (CjSystemConst.CjCompareJobResultDiffType.Add.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(paragraph1, CjSystemConst.CjCompareJobResultColor.Del);
            PoiWordUtil.assignColor(paragraph2, CjSystemConst.CjCompareJobResultColor.Add);
        } else if (CjSystemConst.CjCompareJobResultDiffType.Del.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(paragraph1, CjSystemConst.CjCompareJobResultColor.Add);
            PoiWordUtil.assignColor(paragraph2, CjSystemConst.CjCompareJobResultColor.Del);
        } else {
            PoiWordUtil.assignColor(paragraph1, CjSystemConst.CjCompareJobResultColor.Diff);
            PoiWordUtil.assignColor(paragraph2, CjSystemConst.CjCompareJobResultColor.Diff);
        }
    }

    /**
     * @param cell1:
     * @param cell2:
     * @param re:
     * @description: 给单元格添加批注
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:56
     * @modify:
     */
    private void addCellComment(XWPFTableCell cell1, XWPFTableCell cell2,
        CjCompareJobResultEntity re, PoiCommentService commentService1,
        PoiCommentService commentService2, String firstImageTitle,
        String secondImageTitle) {
        commentService1.insertCommentToCell(cell1, secondImageTitle + re.getDoccontent2());
        commentService2.insertCommentToCell(cell2, firstImageTitle + re.getDoccontent1());
        if (CjSystemConst.CjCompareJobResultDiffType.Add.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(cell1, CjSystemConst.CjCompareJobResultColor.Del);
            PoiWordUtil.assignColor(cell2, CjSystemConst.CjCompareJobResultColor.Add);
        } else if (CjSystemConst.CjCompareJobResultDiffType.Del.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(cell1, CjSystemConst.CjCompareJobResultColor.Add);
            PoiWordUtil.assignColor(cell2, CjSystemConst.CjCompareJobResultColor.Del);
        } else {
            PoiWordUtil.assignColor(cell1, CjSystemConst.CjCompareJobResultColor.Diff);
            PoiWordUtil.assignColor(cell2, CjSystemConst.CjCompareJobResultColor.Diff);
        }
    }

    /**
     * @param row1:
     * @param row2:
     * @param re:
     * @description: 给行添加批注
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:56
     * @modify:
     */
    private void addRowComment(XWPFTableRow row1, XWPFTableRow row2, CjCompareJobResultEntity re,
        PoiCommentService commentService1, PoiCommentService commentService2,
        String firstImageTitle,
        String secondImageTitle) {
        commentService1.insertCommentToRow(row1, secondImageTitle + re.getDoccontent2());
        commentService2.insertCommentToRow(row2, firstImageTitle + re.getDoccontent1());
        if (CjSystemConst.CjCompareJobResultDiffType.Add.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(row1, CjSystemConst.CjCompareJobResultColor.Del);
            PoiWordUtil.assignColor(row2, CjSystemConst.CjCompareJobResultColor.Add);
        } else if (CjSystemConst.CjCompareJobResultDiffType.Del.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(row1, CjSystemConst.CjCompareJobResultColor.Add);
            PoiWordUtil.assignColor(row2, CjSystemConst.CjCompareJobResultColor.Del);
        } else {
            PoiWordUtil.assignColor(row1, CjSystemConst.CjCompareJobResultColor.Diff);
            PoiWordUtil.assignColor(row2, CjSystemConst.CjCompareJobResultColor.Diff);
        }
    }

    /**
     * @param table1:
     * @param table2:
     * @param re:
     * @description: 给表格添加批注
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:57
     * @modify:
     */
    private void addTableComment(XWPFTable table1, XWPFTable table2, CjCompareJobResultEntity re,
        PoiCommentService commentService1, PoiCommentService commentService2,
        String firstImageTitle,
        String secondImageTitle) {
        commentService1.insertCommentToTable(table1, secondImageTitle + re.getDoccontent2());
        commentService2.insertCommentToTable(table2, firstImageTitle + re.getDoccontent1());
        if (CjSystemConst.CjCompareJobResultDiffType.Add.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(table1, CjSystemConst.CjCompareJobResultColor.Del);
            PoiWordUtil.assignColor(table2, CjSystemConst.CjCompareJobResultColor.Add);
        } else if (CjSystemConst.CjCompareJobResultDiffType.Del.equals(re.getDifftype())) {
            PoiWordUtil.assignColor(table1, CjSystemConst.CjCompareJobResultColor.Add);
            PoiWordUtil.assignColor(table2, CjSystemConst.CjCompareJobResultColor.Del);
        } else {
            PoiWordUtil.assignColor(table1, CjSystemConst.CjCompareJobResultColor.Diff);
            PoiWordUtil.assignColor(table2, CjSystemConst.CjCompareJobResultColor.Diff);
        }
    }

    /**
     * @param document:
     * @param outFile:
     * @description: 保存文件
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 13:54
     * @modify:
     */
    private void saveFile(XWPFDocument document, String outFile) {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(outFile);
            document.write(out);
            out.close();
        } catch (Exception ex) {
            throw new ServiceException(ex);
        } finally {
            closeStream(out);
        }
    }

    /**
     * @param obj:
     * @description: 关闭流
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 14:01
     * @modify:
     */
    private void closeStream(Closeable obj) {
        try {
            if (obj != null) {
                obj.close();
            }
        } catch (Exception ex) {

        }
    }

    /**
     * @param file:
     * @description: 删除文件
     * @return: void
     * @author: zhanglongfei
     * @date: 2023/2/24 15:05
     * @modify:
     */
    private void deleteFile(File file) {
        try {
            if (file != null) {
                file.delete();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
